package app.service.goods;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import app.model.goods.GoodsShoppingcart;
import base.PageInfo;
import base.dao.HibernateDao;
import base.service.BaseService;

@Service
public class GoodsShoppingcartService extends BaseService {

	@Autowired
	private HibernateDao<GoodsShoppingcart> hibernateDao;
	
	@Override
	public HibernateDao<GoodsShoppingcart> getDao() {
		return this.hibernateDao;
	}
	
	/**
	 * 查询购物车
	 * @param pageInfo
	 * @return
	 */
	public PageInfo query(PageInfo pageInfo) {
		String sql = "SELECT a.* FROM goods_shoppingcart a,goods_base_use_view b WHERE a.goods_id=b.id";
		PageInfo page = getDao().queryPageListBySql(pageInfo, sql);
		return page;
	}

	public void saveEntity(GoodsShoppingcart entity) {
		entity.setBuyerId("");
		entity.setBuyerName("");
		getDao().save(entity);
	}

	public void updateEntity(GoodsShoppingcart entity) {
		getDao().mergeAndUpdate(entity);
	}

	public GoodsShoppingcart queryEntityById(String id) {
		String sql = "select * from goods_shoppingcart a where a.id = '"+id+"'";
		Object entity = getDao().queryEntityBySql(sql , GoodsShoppingcart.class);
		return (GoodsShoppingcart)entity;
	}

	public void delEntity(String ids) {
		if(!StringUtils.isEmpty(ids)){
			String[] idsArray = ids.split(",");
			for (int i = 0; i < idsArray.length; i++) {
				GoodsShoppingcart gb = new GoodsShoppingcart();
				gb.setId(idsArray[i]);
				getDao().del(gb);
			}
		}
	}
}
